from flaskapp.util.Sqlalchemy import db

class Consultation(db.Model):
    __tablename__ = 'consultation'

    consultation_id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    submitted_at = db.Column(db.DateTime, default=db.func.current_timestamp())
    status = db.Column(db.Enum('未处理', '处理中','已完成'), default='未处理')
    processed_at = db.Column(db.DateTime)
    reply = db.Column(db.Text)

    def __init__(self, user_id, content, status='未处理', submitted_at=None, reply=None):
        self.user_id = user_id
        self.content = content
        self.status = status
        self.submitted_at = submitted_at or db.func.current_timestamp()
        self.reply = reply
    
    def to_dict(self):
        return {
            'consultation_id': self.consultation_id,
            'user_id': self.user_id,
            'content': self.content,
            'submitted_at': self.submitted_at.isoformat() if self.submitted_at else None,
            'status': self.status,
            'processed_at': self.processed_at.isoformat() if self.processed_at else None,
            'reply': self.reply
        }